package com.micro.ai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.ai.auth.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 角色Mapper接口
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 根据用户ID查询角色列表
     * 
     * @param userId 用户ID
     * @return 角色列表
     */
    @Select("SELECT r.* FROM roles r " +
            "INNER JOIN user_roles ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND r.status = 'active' AND r.deleted_at IS NULL")
    List<Role> selectByUserId(@Param("userId") String userId);

    /**
     * 根据租户ID和角色编码查询角色
     * 
     * @param tenantId 租户ID
     * @param code 角色编码
     * @return 角色信息
     */
    @Select("SELECT * FROM roles WHERE tenant_id = #{tenantId} AND code = #{code} AND deleted_at IS NULL")
    Role selectByTenantAndCode(@Param("tenantId") String tenantId, @Param("code") String code);
}

